home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Net / Utilities / Seer family 2.0 / Timing / timer.asm next >
Encoding:
Assembly Source File  |  1987-08-29  |  3.4 KB  |  113 lines  |  [TEXT/QED1]

  1. ;    Millisecond timer mini-driver
  2. ;    Written by Scott Gillespie
  3. ;    in the Reed College MacLab V85.01.14
  4. ;
  5. ;      Modified on 6-Oct-1986 by 
  6. ;      David Powell, Research Services Branch, NIH
  7. ;      Building 36, Room 2A03
  8. ;      Bethesda, MD  20892
  9. ;
  10. ;      Converted .PROC and .LONG to XDEF and DC.L for MDS
  11. ;      Bug Fix: Reversed order of Unlk and Movem in Exit.
  12. ;
  13. ;    This code contains a miniature driver for doing millisecond
  14. ;    timing on the Macintosh.  The 6522 (VIA) chip's Timer1 is
  15. ;    loaded with a value that causes an interrupt every millisecond.
  16. ;    A LongInt variable is incremented after each interrupt.  The
  17. ;    variable can then be read to determine how many milliseconds
  18. ;    passed since the timer was started.
  19. ;
  20. ;    Note 1:  VIA's Timer1 is also used by the sound driver, so any
  21. ;    use of the sound driver will temporarily stop the counter.
  22. ;
  23. ;    Note 2:  Be sure to turn off the timer when you are through 
  24. ;    with it.
  25. ;
  26. ;      The mini-driver consists of four routines: 
  27. ;
  28. ;      InitTimer -- Set up timer; call once at beginning of your program
  29. ;      KillTimer -- Turn off timer and restore Sound driver handler
  30. ;      ResetTimer -- Set counter to zero
  31. ;      count:=TimerValue -- Get current millisecond count
  32. ;
  33. ;    VIA input = systemclock / 10 = 783,360 cycles/sec
  34. ;    VIA Timer1 value for 1 ms = 782 (783-1) = 3 * 256 + 14
  35. ;
  36. ;      To understand all this, it helps to have the 6522 manual
  37. ;      which describes Timer1 operation, plus IM V3 Ch 2 (Mac
  38. ;      Hardware) and IM V2 Ch 6 (Device Manager).  See especially 
  39. ;      pp III-39 to III-45 on the VIA and II-195 to II-200 on 
  40. ;      interrupt handlers.
  41. ;
  42.     .Listtofile timer.Lst
  43.     
  44. LowCount    equ    14    ; low byte, Timer1
  45. HighCount    equ    3    ; high byte, Timer1
  46.  
  47. Lvl1DT        equ    $192    ; From SYSEQU.TXT
  48. VIA        equ    $1d4
  49. vT1C        equ    $800
  50. vT1CH        equ    $A00
  51. vT1L        equ    $C00
  52. vACR        equ    $1600
  53. vIER        equ    $1C00
  54.  
  55.     xdef    InitTimer,ResetTimer,TimerValue,KillTimer
  56.  
  57. InitTimer
  58.     movem.l    a0-a1,-(sp)    ; Save registers
  59.     movea.l    #Lvl1dt,a0    ; Save Level 1 dispatch table
  60.     lea    LastVIA,a1    ; interrupt handler for later
  61.     move.l    24(a0),(a1)
  62.     lea    VIArout,a1    ; Install my interrupt handler
  63.     move.l    a1,24(a0)    ; in dispatch table
  64.     movea.l    VIA,a1        ; Get VIA address and set up Timer1
  65.     ori.b    #$40,VACR(a1)    ; in free-run mode
  66.     move.b    #$c0,VIER(a1)    ; Enable VIA interrupts ($80+$40)
  67.     move.b    #LowCount,VT1l(a1)    ; Load low latch
  68.     move.b    #HighCount,VT1CH(a1)    ; Load high counter/latch
  69.     lea    counter,a0    ; Get counter address
  70.     clr.l    (a0)        ; Zero counter
  71.     movem.l    (sp)+,a0-a1    ; Restore registers
  72.     rts            ; Return
  73.  
  74.     
  75. ResetTimer
  76.     move.l    a0,-(sp)    ; Save register
  77.     lea    counter,a0    ; Get counter address
  78.     clr.l    (a0)        ; Zero counter
  79.     move.l    (sp)+,a0    ; Restore register
  80.     rts            ; Return
  81.     
  82. KillTimer
  83.     movem.l    a0-a1,-(sp)    ; Save registers
  84.     movea.l    VIA,a1        ; Disable VIA interrupts
  85.     move.b    #$40,VIER(a1)
  86.     lea    LastVIA,a1    ; Restore previous interrupt handler
  87.     movea.l    #Lvl1dt,a0    ; in level 1 dispatch table
  88.     move.l    (a1),24(a0)
  89.     movem.l    (sp)+,a0-a1    ; Restore registers
  90.     rts            ; Return
  91.  
  92. timerValue
  93.     move.l    a0,-(sp)    ; Save register
  94.     lea    counter,a0    ; Get counter address
  95.     move.l    (a0),8(sp)    ; Return counter value
  96.     move.l    (sp)+,a0    ; Restore register
  97.     rts            ; Return
  98.  
  99. ;
  100. ;    This is the VIA Timer1 interrupt handler -- the VIA address is
  101. ;    passed in A1 by the Mac primary interrupt handler.
  102. ;
  103. VIArout
  104.     move.b    VT1C(a1),d0    ; Clear interrupt flag on VIA
  105.     lea    counter,a0    ; Get counter address
  106.     addq.l    #1,(a0)        ; Increment counter
  107.     rts
  108.  
  109. Counter    dc.l    1        ; Millisecond counter
  110. LastVIA    dc.l    1        ; Save last interrupt handler here.
  111.  
  112.     end
  113.